<include file="Common:pony" />
<body>
<include file="Common:sellerpanel" />
<h2>待提交订单</h2>
<form id="form1" name="form1" method="post" action="<{:U('tradesaveall')}>">
<table width="100%" cellspacing="1" cellpadding="3" border="0">
<tr>
	<td width="80">搜索订单</td>
	<td width="600">
		按地区筛选：<select name="did" id="did"><option value="">选择地区</option><{$district}></select>
		<input type="button" name="button1" id="btnFilter" value="筛选" />
		选择快递:<select name="tplid" id="tplid"><option value="0">选择快递</option><{$tploptions}></select>
		<input type="submit" name="submit" id="btnTradeSaveAll" value="批量提交订单" />
	</td>
	<td><span style="font-family: 微软雅黑, Tahoma; letter-spacing: 2px; display: none;" id="result"></span>
	</td>
</tr>
</table>
<table width="100%" cellspacing="1" cellpadding="0" border="0" bgcolor="#999999">
<tr class="header">
<th width="50" height="30" align="center"></th>
<th width="120" height="30" align="center">订单号</th>
<th width="120" align="center">买家帐号</th>
<th align="center">收货地址</th>
<th width="120" align="center">卖家备注<br>
  买家留言</th>
<th width="130" align="center">商品信息</th>
<th width="60" align="center">邮费</th>
<th width="60" align="center">物流</th>
<th width="100" align="center">快递</th>
<th align="center">订单金额<br>
  总成本</th>
<th width="100" align="center">操作</th>
</tr>

<php>

if( $trade_mergeR ) {
	$merged = false;
	echo '<tr class="header"><td colspan="11" height="30" align="center"><div>存在可合并订单<a name="tm"></a></div></td></tr>';
	foreach( $trade_mergeR as $trade ) {
			$tid = $trade['tid'];
			$systrade = D('Trade')->where("tid={$tid}")->find();
			if( $systrade ) {
				$merged = true;
				continue;
			}else {
				$merged = false;
			}
			$ordersinfo = '';
			foreach( $trade['orders']['order'] as $t=>$order ){
				$ordersinfo .= '<div>'. ($t+1).'.'. $order['outer_sku_id'] .'('.$order['num'] .'件)</div>';
			}
			if( ! empty( $refundTrade ) ) {
			$tidsArr = explode( ',', $trade['tids'] );
				foreach( $tidsArr  as $tidT ) {
					if( array_key_exists( $tidT, $refundTrade ) ) {
						$ordersinfo .= '<span style="color:red">'. $tidT .':退款中</span>';
					}
				}
			}
			$chengben = $trade['payment'] ;
			$tradeflag = tradeflag( $trade['seller_flag'] );
			echo '<tr class="row"><td colspan="11" height="30">合并的tid：'.$trade['systids'] .'</td></tr>
			<tr class="row">
			<td></td>
			<td align="center">'. number_format($trade['tid'], '0','','') .'</td>
			<td align="center">'. $trade['buyer_nick'] .'<br>'. contacttuser( $trade['buyer_nick'] ) . '</td>
			<td>'. $trade['receiver_name'] .','. $trade['receiver_mobile'] . ','. $trade['receiver_phone'] .'<br>' . $trade['receiver_state'] .', '. $trade['receiver_city']. ' '. $trade['receiver_district'] .','. $trade['receiver_address'] .','. $trade['receiver_zip'] . '</td>
			<td align="center"><span style="color:red">'. $tradeflag . ' '. $trade['seller_memo'] . '</span><br><span style="color:blue">' . $trade['buyer_message'] .'</span></td>
			<td>'. $ordersinfo .'</td>
			<td>'.$trade['post_fee'].'</td>
			<td>'.$trade['shipping_type'].'</td>
			<td align="center"><select name="company[]" id="Mcompany_'. number_format( $trade['tid'], 0, '', '' ).'" alt="'. number_format( $trade['tid'], '0', '', '').'"><option value="0">选择快递</option>' .$tploptions. '</select></td>
			<td align="center">' .$trade['payment']. '/<span id="chengbenM_'. number_format( $trade['tid'], 0, '', '').'"></span></td>
			<td align="center"><input type="button" id="MbtnPostTrade_'. number_format( $trade['tid'], 0, '','' ).'" alt="'. number_format( $trade['tid'], '0', '', '').'" value="合并提交订单" tids="'.$trade[tids].'"/><input type="hidden" value="'. $trade['receiver_state']. '" name="stateM_'. number_format( $trade['tid'], 0, '', '' ) .'" id="stateM_'. number_format( $trade['tid'], 0, '', '' ) .'"> </td></tr>';
			//<textarea id="tradedataM_'. number_format( $trade['tid'], '0', '', '').'" name="tradedata[]" style="display:none">'. serialize( $trade ). '</textarea>	
		}
	if( $merged == true ) {
		echo '<tr class="row"><td colspan="11" height="30" align="center">已处理合并订单</td></tr>';
	}
	
	
	
}
unset( $trade );
</php>
<tr class="header">
<th width="50" height="30" align="center"><input type="checkbox" name="tidall" id="tidall"><label for="tidall">全选</label></th>
<th width="120" height="30" align="center">订单号</th>
<th width="120" align="center">买家帐号</th>
<th align="center">收货地址</th>
<th width="120" align="center">卖家备注<br>
  买家留言</th>
<th width="130" align="center">商品信息</th>
<th width="60" align="center">邮费</th>
<th width="60" align="center">物流</th>
<th width="100" align="center">快递</th>
<th align="center">订单金额<br>
  总成本</th>
<th width="100" align="center">操作</th>
</tr>
<php>

if( $orders ) {
	foreach( $orders['trades_sold_get_response']['trades']['trade'] as $trade ) {
		//$systrade = D('Trade')->where("tid='{$trade['tid']}' OR systids like '%{$trade['tid']}%'")->find();
		//if( $systrade ) continue;
		$ordersinfo = '';		
		foreach( $trade['orders']['order'] as $t=>$order ){
			$ordersinfo .= '<div>'. ($t+1).'.'. $order['outer_sku_id'] .'('.$order['num'] .'件)</div>';
		}
		if( $refundTrade[ number_format( $trade['tid'], 0, '', '' )  ] ) $ordersinfo .= ' <span style="color:red">退款中</span>';
		$chengben = $trade['payment'] ;
		$tradeflag = tradeflag( $trade['seller_flag'] );
		echo '<tr class="row">
		<td height="50" align="center"><input type="checkbox" name="tid[]" id="tid_'. number_format( $trade['tid'], 0, '', '' ) . '" value="'. number_format( $trade['tid'], 0, '', '' ) . '" /></td>
		<td height="50" align="center">'. number_format($trade['tid'], '0','','') . '</td>
		<td align="center">'. $trade['buyer_nick'] .'<br>'. contacttuser( $trade['buyer_nick'] ) . '</td>
		<td>'. $trade['receiver_name'] .','. $trade['receiver_mobile'] . ','. $trade['receiver_phone'] .'<br>' . $trade['receiver_state'] .', '. $trade['receiver_city']. ' '. $trade['receiver_district'] .','. $trade['receiver_address'] .','. $trade['receiver_zip'] . '</td>
		<td align="center"><span style="color:red">'. $tradeflag . ' '. $trade['seller_memo'] . '</span><br><span style="color:blue">' . $trade['buyer_message'] .'</span></td>
		<td>'. $ordersinfo .'</td>
		<td>'.$trade['post_fee'].'</td>
		<td>'.$trade['shipping_type'].'</td>		
		<td align="center"><select name="company[]" id="company_'. number_format( $trade['tid'], 0, '', '' ).'" alt="'. number_format( $trade['tid'], '0', '', '').'"><option value="0">选择快递</option>' .$tploptions. '</select></td>
		<td align="center">' .$trade['payment']. '/<span id="chengben_'. number_format( $trade['tid'], 0, '', '').'"></span></td>
		<td align="center"><input type="button" id="btnPostTrade_'. number_format( $trade['tid'], 0, '','' ).'" alt="'. number_format( $trade['tid'], '0', '', '').'" value="提交订单" /><input type="hidden" value="'. $trade['receiver_state']. '" name="state_'. number_format( $trade['tid'], 0, '', '' ) .'" id="state_'. number_format( $trade['tid'], 0, '', '' ) .'"> </td></tr>';
	} 
	//<textarea id="tradedata_'. number_format( $trade['tid'], '0', '', '').'" name="tradedata[]" style="display:none">'. serialize( $trade ). '</textarea>
	//print_r( $orders );
}

unset( $order );
</php>



<tr class="row">
<td colspan="11" height="30" align="center"><div><{$page}></div>
<input id="p" name="p" type="hidden" value="<{$p}>">
</td>
</tr>
</table>
</form>
<script>
$(document).ready(function(){
	$("select[id^='company_']").bind('change', function(){
		var tplid = $(this).val();
		var tid   = $(this).attr( 'alt' );
		//trade : $("#tradedata_" + tid ).val() ,
		$.post ( '<{:U('Index/price')}>', {  tplid: tplid, receiver_state : $("#state_" + tid ).val() , tid : tid, p: $("#p").val() }  ,
			function(data){
				if( 1 == data['status'] ) {
					$('#chengben_'+tid).html( data['data']['cost'] );	
				} else {
					alert( data['info']);
				}
			 	
			}	
		); 
	});
	
	
	
	$("input[id^='btnPostTrade']").bind('click', function(){
		var _this = $(this);
		var tid = $(this).attr( 'alt' );
		//console.log( tid );
		//return ;
		var tplid = parseInt( $( '#company_'+tid ).val() );
		if( null == tplid || 0 == tplid ) {
			alert('选择快递模板');
			return false ;
		}
		//trade : $("#tradedata_" + tid ).val(),
		$.post( '<{:U('Index/tradesave')}>' , {
				 tplid: tplid , tid : tid, p : $("#p").val()
			}, 
			function(data){
				if( 1 == data['status'] ){
					//alert( data['info'] );
					_this.val( data['info'] ).attr('disabled', 'disabled');
				}else {
					alert('提交失败 '+ data['info'] );	
				}
				
			} 
		);
	} );
	
	
	//筛选订单
	
	$("#btnFilter").bind( 'click', function(){
		window.location.href= $("#did").val();
	} )
	<?php
		if( $did ) {
			echo '$("#did").val( "'. U('Index/tradelist?did='.$did) .'")';	
		} 
	?>
	
	//合并的订单的选择快递
	$("select[id^='Mcompany_']").bind('change', function(){
		var tplid = $(this).val();
		var tid   = $(this).attr( 'alt' );
		//trade : $("#tradedataM_" + tid ).val() ,
		$.post ( '<{:U('Index/price')}>', {  tplid: tplid, receiver_state : $("#stateM_" + tid ).val(), tid : tid, p: $("#p").val()  }  ,
			function(data){
				if( 1 == data['status'] ) {
					$('#chengbenM_'+tid).html( data['data']['cost'] );	
				} else {
					alert( data['info']);
				}
			 	
			}	
		); 
	});
	
	
	$("input[id^='MbtnPostTrade']").bind('click', function(){
		var _this = $(this);
		var tid = $(this).attr( 'alt' );
		var tplid = parseInt( $( '#Mcompany_'+tid ).val() );
		var tids = _this.attr('tids');
		var tidsarr = tids.split(',');
		if( null == tplid || 0 == tplid ) {
			alert('选择快递模板');
			return false ;
		}
		if( !confirm( '确定要合并提交吗？')  ){ return false ;}
		
		$.post( '<{:U('Index/tradesave')}>' , {
				trade : $("#tradedataM_" + tid ).val(), tplid: tplid , tid : tid, p: $("#p").val() , m :1 
			}, 
			function(data){
				if( 1 == data['status'] ){
					//alert( data['info'] );
					for( var i =0; i< tidsarr.length ; i++ ) {
						$("#btnPostTrade_"+ tidsarr[i]).val( data['info'] ).attr('disabled', 'disabled');
					}
					_this.val( data['info'] ).attr('disabled', 'disabled');
				}else {
					alert('提交失败 '+ data['info'] );	
				}
				
			} 
		);
		
	} );
	
	
	//
	//全选
	$(":checkbox[id^=tid_]").click( function() {
		chcekstyle( $(this) ); 
		
	} );
	function chcekstyle ( $checkbox ) {
		var tr = $checkbox.parent().parent();
		var checked = $checkbox.attr("checked");
		if( checked ) {
			tr.css('background', '#CCFFCC');
		}else {
			tr.css('background', '');
		}
		
	}
	$("#tidall").click( function () {
		$(":checkbox[id^=tid_]").attr("checked", $(this).attr("checked") );
		$(":checkbox[id^=tid_]").each( function( i ){
			chcekstyle( $(this) ); 
			
		} );
	} );
	
	
	
	
	$('#form1').ajaxForm({
		beforeSubmit : checkForm, // pre-submit callback
		success : complete, // post-submit callback
		dataType : 'json'
	});
	function checkForm() {
		
		var error = '';
		var tids = $(":input[id^='tid_']:checked");
		var tplid = $("#tplid").val();
		if( tids.length == 0 ) { 
			error = '订单未选择';
		}
		if( tplid == '' || tplid == 0 ){
			error = '快递模板未选择';
		}
		 
		if( error != '' ) {
			$('#result').html( error ).show().fadeOut(2000);
			$("#btnTradeSaveAll").removeAttr( "disabled" ).val("批量提交订单");
			return false;
		}else{
			$("#btnTradeSaveAll").attr( "disabled" , "disabled").val("正在批量提交订单...");
		}
		//可以在此添加其它判断	
	}
	function complete(data) {
		if (data.status == 1) {
			$('#result').html(data.info).show();
			var tids = data.data;
			if( tids ) {
				var tidsarr = tids.split(',');
				for( var i =0; i< tidsarr.length ; i++ ) {
					$("#btnPostTrade_"+ tidsarr[i]).attr('disabled', 'disabled');	//不能点击
					$("#btn_"+ tidsarr[i]).removeAttr( 'checked' );										//取消选中
				}
			}
			
		} else {
			$('#result').html(data.info).show();
		}
		$("#btnTradeSaveAll").removeAttr( "disabled" ).val("批量提交订单");
	}
	
	
	
});
</script>
</body>
</html>